import { Dust, Fire, Splash } from "./particle.js";
let states = {
    'SITTING': 0,
    'JUMPING': 1,
    'RUNNING': 2,
    'FALLING': 3,
    'ROLLING': 4,
    'DIVING': 5,
    'HIT': 6
}

class State {
    constructor(state, game, player) {
        this.state = state;
        this.game = game;
        this.player = player;
    }
}
export class Sitting extends State {
    constructor(game, player) {
        super('SITTING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 3;
        this.player.frameY = 5;
    }
    handleInput(input) {
        if (input.includes('ArrowLeft') || input.includes('ArrowRight'))
            this.player.setState(states.RUNNING, 1)
        else if (input.includes('ArrowUp'))
            this.player.setState(states.JUMPING, 1)
        else if (input.includes('Enter'))
            this.player.setState(states.ROLLING, 2)

    }
}
export class Running extends State {
    constructor(game, player) {
        super('RUNNING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 7;
        this.player.frameY = 3;
    }
    handleInput(input) {
        //添加烟尘粒子效果
        this.game.particles.unshift(new Dust(this.game, this.player.x + this.player.width * 0.5, this.player.y + this.player.height * 0.5))
        if (input.includes('ArrowDown'))
            this.player.setState(states.SITTING, 0)
        else if (input.includes('ArrowUp'))
            this.player.setState(states.JUMPING, 1)
        else if (input.includes('Enter'))
            this.player.setState(states.ROLLING, 2)
    }
}
export class Jumping extends State {
    constructor(game, player) {
        super('JUMPING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 5;
        this.player.frameY = 1;

    }
    handleInput(input) {
        if (this.player.onGround()) this.player.speedY = - 25

        if (this.player.speedY >= 0)
            this.player.setState(states.FALLING, 1)

        if (input.includes('Enter'))
            this.player.setState(states.ROLLING, 2)

        if (input.includes('ArrowDown')) {
            this.player.setState(states.DIVING, 1);
        }

    }
}
export class Falling extends State {
    constructor(game, player) {
        super('FALLING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 5;
        this.player.frameY = 2;

    }
    handleInput(input) {
        if (this.player.onGround()) {
            this.player.setState(states.RUNNING, 1)

        }
        // if (input.includes('Enter'))
        //     this.player.setState(states.ROLLING, 2)

    }
}
export class Rolling extends State {
    constructor(game, player) {
        super('ROLLING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 5;
        this.player.frameY = 6;

    }
    handleInput(input) {
        //添加火焰粒子
        this.game.particles.unshift(new Fire(this.game, this.player.x + this.player.width * 0.5, this.player.y + this.player.height * 0.5))
        if (!input.includes('Enter') && this.player.onGround()) {
            this.player.setState(states.RUNNING, 1)
        } else if (!input.includes('Enter') && !this.player.onGround()) {
            this.player.setState(states.FALLING, 1)
        } else if (input.includes('ArrowUp') && this.player.onGround()) {
            this.player.speedY = - 25
        } else if (input.includes('ArrowDown')&&!this.player.onGround()) {
            this.player.setState(states.DIVING, 1);
        }


    }
}

export class Diving extends State {
    constructor(game, player) {
        super('DIVING', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 5;
        this.player.frameY = 6;
        this.player.speedY = 15;

    }
    handleInput(input) {
        //添加火焰粒子
        this.game.particles.unshift(new Fire(this.game, this.player.x + this.player.width * 0.5, this.player.y + this.player.height * 0.5))
        if (this.player.onGround()) {

            for (let i = 0; i < 30; i++) {
                this.game.particles.unshift(new Splash(this.game, this.player.x + this.player.width * 0.5, this.player.y + this.player.height * 0.5))
            }
            this.player.setState(states.RUNNING, 1)
            if (input.includes('Enter'))
                this.player.setState(states.ROLLING, 2)
        }


    }
}

export class Hit extends State {
    constructor(game, player) {
        super('HIT', game, player);
    }

    enter() {
        this.player.frameX = 0;
        this.player.maxFrame = 9;
        this.player.frameY = 4;

    }
    handleInput(input) {
        if (this.player.frameX == this.player.maxFrame ) {
            if (this.player.onGround()) {
                this.player.setState(states.RUNNING, 1)
            }
            else {
                this.player.setState(states.FALLING, 1)

            }
        }
    }



}
